package com.zhen.controller;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.zhen.domain.User;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
public class FileUploadController {

    @RequestMapping(value="/{formName}")
    public String loginForm(@PathVariable String formName){
        // 动态跳转页面
        return formName;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public String upload(HttpServletRequest request, @RequestParam String description,@RequestParam MultipartFile file) throws IOException {
        System.out.println(description);
        if(!file.isEmpty()){
            String path = request.getServletContext().getRealPath("/images/");
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }

            file.transferTo(new File(path + File.separator + filename));
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping(value = "/register")
    public String register(HttpServletRequest request, @ModelAttribute User user, Model model) throws IOException {
        System.out.println(user.getUsername());
        if(!user.getImage().isEmpty()){
            String path = request.getServletContext().getRealPath("/images/");
            String filename = user.getImage().getOriginalFilename();
            File filepath = new File(path,filename);
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            user.getImage().transferTo(new File(path + File.separator + filename));
            model.addAttribute(user);
            return "userInfo";
        }else{
            return "error";
        }
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam String filename,Model model) throws IOException {
        String path = request.getServletContext().getRealPath("/images/");
        File file = new File(path + File.separator + filename);
        HttpHeaders headers = new HttpHeaders();
        String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
        headers.setContentDispositionFormData("attachment",downloadFileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
